﻿using ImTools;
using Prism.Commands;
using Prism.Common;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WheelClean2.Model;
using WheelClean2.Mould;

namespace WheelClean.ViewModels
{
    class LaserControlViewModel : BindableBase, IDialogAware
    {

      

        public event Action<IDialogResult> RequestClose;

        private LaserInfo _infos;
        /// <summary>
        /// 激光工艺实体
        /// </summary>
        public LaserInfo Info
        {
            get { return _infos; }
            set { SetProperty(ref _infos, value); }
        }

        private List<MarkInfo> _marks;
        /// <summary>
        /// 板卡集合
        /// </summary>
        public List<MarkInfo> Marks
        {
            get { return _marks; }
            set { SetProperty(ref _marks, value); }
        }

        private string _status;
        /// <summary>
        /// 板卡链接状态
        /// </summary>
        public string Status
        {
            get { return _status; }
            set { SetProperty(ref _status, value); }
        }
        private ObservableCollection<LaserInfo> _lists;
        /// <summary>
        /// 设备列表
        /// </summary>
        public ObservableCollection<LaserInfo> Lists
        {
            get { return _lists; }
            set { SetProperty(ref _lists, value); }
        }

        private List<bool> _selectItems;
        /// <summary>
        /// 是否
        /// </summary>
        public List<bool> SelectItems
        {
            get { return _selectItems; }
            set { SetProperty(ref _selectItems, value); }
        }


        public string Title => "激光控制";

        /// <summary>
        /// 开始
        /// </summary>
        public DelegateCommand StartCommand { get; private set; }
        /// <summary>
        /// 开始
        /// </summary>
        public DelegateCommand StopCommand { get; private set; }
        /// <summary>
        /// 结束
        /// </summary>
        public DelegateCommand FinishCommand { get; private set; }
        /// <summary>
        /// 红光预览开启
        /// </summary>
        public DelegateCommand PreviewStartCommand { get; private set; }

        /// <summary>
        /// 红光预览关闭
        /// </summary>
        public DelegateCommand PreviewStopCommand { get; private set; }
        public DelegateCommand DelegateCommand_Save { get; private set; }
        private AppDbContext _content;
        private DaBiaoHelper daBiaoHelper;

        LaserControlViewModel()
        {
            _content = new AppDbContext();
            DelegateCommand_Save = new DelegateCommand(Save);
            daBiaoHelper = DaBiaoHelper.Instance;
            StartCommand = new DelegateCommand(Start);
            StopCommand = new DelegateCommand(Stop);
            FinishCommand = new DelegateCommand(Finish);
            PreviewStartCommand = new DelegateCommand(PreviewStart);
            PreviewStopCommand = new DelegateCommand(PreviewStop);
            Marks = daBiaoHelper.Marks;
            Status = daBiaoHelper.Status;
            if (daBiaoHelper.Marks==null|| daBiaoHelper.Marks.Count==0)
            {
                Marks = new List<MarkInfo>();
                Marks.Add(new MarkInfo(new CardInfo()));
                Marks.Add(new MarkInfo(new CardInfo()));
            }
            Lists = new ObservableCollection<LaserInfo>();
            Lists.AddRange(_content.LaserInfos.ToList());
            SelectItems = new List<bool>() { true, false };

        }
        /// <summary>
        /// 重连
        /// </summary>
        //private void ReLink()
        //{
        //    foreach (var markInfo in Marks)
        //    {
        //        markInfo.PreviewStart();
        //    }
        //}
        private void PreviewStart()
        {
            foreach (var markInfo in Marks)
            {
                markInfo.PreviewStart();
            }
        }
        private void PreviewStop()
        {
            foreach (var markInfo in Marks)
            {
                markInfo.PreviewStop();
            }
        }

        private void Start()
        {
            foreach (var markInfo in Marks)
            {
                markInfo.Start();
            }
        }

        private void Stop()
        {
            foreach (var markInfo in Marks)
            {
                markInfo.Stop();
            }
        }

        private void Finish()
        {
            foreach (var markInfo in Marks)
            {
                markInfo.Finish();
            }
        }

        private void Save()
        {
            if(Info.ID==0)
            {
                _content.LaserInfos.Add(Info);
            }
            else
            {
                _content.SaveChanges();
            }
           
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
          
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
           //if(parameters.ContainsKey("type"))
           // {
           //     string type = parameters.GetValue<string>("type");
           //     if(type=="add")
           //     {
           //         Info = new LaserInfo();
           //     }
           //     else
           //     {
           //         Info = _content.LaserInfos.FindFirst(t => t.ID == Convert.ToInt32(type));
           //     }
           // }
        }
    }
}
